let express = require ('express');
let path = require ('path');
let favicon = require ('serve-favicon');
let winston = require ('./src/server/utils/logger/Logger').winston;
let cookieParser = require ('cookie-parser');
let bodyParser = require ('body-parser');
let index = require ('./src/server/routes/index');
let maven = require ('./src/server/routes/MavenServer');
let wechat = require ('./src/server/routes/WechatServer');
let app = express ();
// view engine setup
app.set ('views', path.join (__dirname, 'src/views'));
app.set ('view engine', 'hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use (bodyParser.json ({ type: 'application/json'  }));
app.use (bodyParser.urlencoded ({extended: false}));
app.use (cookieParser ());
app.use (express.static (path.join (__dirname, 'public')));
app.use (require ('./src/server/utils/logger/RequestLogger').create (winston));
app.use ('/', index);
app.use ('/bizservice', maven);
app.use ('/wechat', wechat);
// catch 404 and forward to error handler
app.use (function ( req, res, next )
{
    let err = new Error ('Not Found');
    err.status = 404;
    next (err);
});
// error handler
app.use (function ( err, req, res )
{
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get ('env') === 'development' ? err : {};
    // render the error page
    res.status (err.status || 500);
    res.render ('error');
});
module.exports = app;
